<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getDetail } from "../api/product.js"

let route = useRoute()
let id = route.params.id
const router = useRouter()

const productDetail = ref({
  product: {
    proName: "",
    proDesc: "",
    proPrice: "",
    proImg: "",
  },
  productImgList: []
}) 

onMounted(() => {
  getProductDetail()
})
//查询详情
async function getProductDetail() {
  const response = await getDetail(id)
  productDetail.value = response.data.data
}
//数量加减
const qty  = ref(1)
function increase() {
  qty.value++
}
function decrease() {
  if(qty.value>1)
    qty.value--
}
//点击去下单，到订单确认页面
async function ToProductConfirm(proId){
  router.push({ path: '/productConfirm', query: { Userid: 1, ProId: proId } })
}
</script>

<template>
  <div class="detail-wrapper">
    <div class="detail-container">
      <!-- 左侧：大图 + 小图 -->
      <div class="image-section">
        <img class="main-img" :src="productDetail.product.proImg" alt="商品主图" />

        <div class="thumb-list">
          <div v-for="img in productDetail.productImgList" :key="img.proImgId" class="thumb">
            <img :src="img.proImgPath" alt="商品图片" />
          </div>
        </div>
      </div>

      <!-- 右侧：商品信息 -->
      <div class="info-section">
        <h2 class="title">{{ productDetail.product.proName }}</h2>
        <p class="desc">{{ productDetail.product.proDesc }}</p>
        <p class="price">￥{{ productDetail.product.proPrice }}</p>

        <!-- 数量选择 -->
        <div class="quantity">
          <span>数量:</span>
          <button @click="decrease">-</button>
          <input type="text" v-model="qty" />
          <button @click="increase">+</button>
        </div>

        <!-- 购买按钮 -->
        <div class="actions">
          <button class="buy-btn" @click="ToProductConfirm(productDetail.product.proId)">去下单</button>
          <button class="cart-btn">加入购物车</button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 外层整体背景 */
.detail-wrapper {
  margin-top: 100px;
  padding: 40px 0;
  background-color: #f9f9f9;
}

/* 内层卡片容器 */
.detail-container {
  display: flex;
  gap: 40px;
  max-width: 1200px;
  margin: auto;
  padding: 30px;
  background: #fff;
  border: 1px solid #eee;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
  font-family: "Arial", sans-serif;
}

/* 左侧图片区 */
.image-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.main-img {
  width: 400px;
  height: 400px;
  object-fit: cover;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  margin-bottom: 20px;
  border: 1px solid #eee;
}

/* 小图横向排列 */
.thumb-list {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.thumb img {
  width: 70px;
  height: 70px;
  object-fit: cover;
  border-radius: 6px;
  cursor: pointer;
  transition: transform 0.2s, border 0.2s;
  border: 2px solid transparent;
  box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}

.thumb img:hover {
  transform: scale(1.1);
  border: 2px solid #42b983;
}

/* 右侧信息区 */
.info-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  padding-right: 20px;
}

.title {
  font-size: 26px;
  margin-bottom: 15px;
  font-weight: bold;
  color: #333;
}

.desc {
  font-size: 16px;
  color: #666;
  margin-bottom: 20px;
  line-height: 1.5;
  border-bottom: 1px dashed #ddd;
  padding-bottom: 10px;
}

.price {
  font-size: 28px;
  font-weight: bold;
  color: #e63946;
  margin-bottom: 25px;
}

/* 数量选择 */
.quantity {
  margin-bottom: 25px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.quantity button {
  width: 32px;
  height: 32px;
  border: none;
  background-color: #42b983;
  color: white;
  border-radius: 6px;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  transition: background-color 0.2s;
}

.quantity button:hover {
  background-color: #369a6e;
}

.quantity input {
  width: 60px;
  text-align: center;
  padding: 6px;
  border: 1px solid #ccc;
  border-radius: 6px;
}

/* 按钮组 */
.actions {
  display: flex;
  gap: 15px;
}

.buy-btn,
.cart-btn {
  padding: 10px 20px;
  font-size: 16px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  transition: 0.2s;
}

.buy-btn {
  background-color: #e63946;
  color: white;
}
.buy-btn:hover {
  background-color: #c72c38;
}

.cart-btn {
  background-color: #42b983;
  color: white;
}
.cart-btn:hover {
  background-color: #369a6e;
}
</style>
